package com.daikun.common.enums;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.daikun.common.iface.SecurityConfigIface;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 加解密场景 id 和基础校验工具枚举类，
 * 字段后面的注释为测试环境可以使用的场景 id
 * @author earl created 2025-05-20
 * @version init 0.0.1
 */
@Slf4j
@Getter
@RequiredArgsConstructor
public enum SecurityTypeEnum implements SecurityConfigIface {
    MOBILE_TYPE(1, "手机号",
            e -> StrUtil.isBlank(e) || (e.length() > 20 && !NumberUtil.isNumber(e) && Base64.isBase64(e)),
            e -> StrUtil.isBlank(e) || e.length() < 20 || NumberUtil.isNumber(e) || !Base64.isBase64(e),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.mobileBatchEncSceneId"), "89"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.mobileSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.mobileBatchDecSceneId"), "")),

    ID_CARD_TYPE(2, "身份证 - 证件号",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.aadhaarBatchEncSceneId"), ""), // 1026
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.aadhaarSingleDecSceneId"), ""), // 1005
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.aadhaarBatchDecSceneId"), "")),

    EMAIL_TYPE(3, "邮箱",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.emailBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.emailSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.emailBatchDecSceneId"), "")),

    BANK_TYPE(4, "银行卡",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.accountBatchEncSceneId"), ""), // 3024
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.accountSingleDecSceneId"), ""), // 3003
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.accountBatchDecSceneId"), "")),

    FACEBOOK_TYPE(5, "facebook",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.facebookBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.facebookSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.facebookBatchDecSceneId"), "")),

    WHATSAPP_TYPE(6, "whatsapp",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.whatsappBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.whatsappSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.whatsappBatchDecSceneId"), "")),

    ZALO_TYPE(7, "zalo",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.zaloBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.zaloSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.zaloBatchDecSceneId"), "")),

    LINE_TYPE(8, "line",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.lineBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.lineSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.lineBatchDecSceneId"), "")),

    TIKTOK_TYPE(9, "tiktok",
            e -> StrUtil.isBlank(e) || (e.startsWith(IS_ENCRYPT_PREFIX) && e.length() == SECURITY_DATA_LENGTH),
            e -> StrUtil.isBlank(e) || !e.startsWith(IS_ENCRYPT_PREFIX) || e.length() != SECURITY_DATA_LENGTH,
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.singleEncSceneId"), "0"),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.tiktokBatchEncSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.tiktokSingleDecSceneId"), ""),
            () -> StrUtil.blankToDefault(SpringUtil.getProperty("scene.tiktokBatchDecSceneId"), "")),
    ;

    /** 唯一编码 */
    private final int code;
    /** 描述信息 */
    private final String desc;
    /** 自定义加密的前置校验 */
    private final Function<String, Boolean> encCheck;
    /** 自定义解密的前置校验 */
    private final Function<String, Boolean> decCheck;
    /** 单个加密的场景 id */
    private final Supplier<String> singleEnc;
    /** 批量加密的场景 id */
    private final Supplier<String> batchEnc;
    /** 单个解密的场景 id */
    private final Supplier<String> singleDec;
    /** 批量解密的场景 id */
    private final Supplier<String> batchDec;

}
